<?php

namespace app\controller;

use app\general\ApiResponse;
use app\general\utils\ConversionUtils;
use app\model\Video;
use app\model\MovieVideo;
use support\Db;
use support\Request;

class VideoController extends BaseController
{
    public function index(Request $request)
    {
        var_dump($request->host());
        $users = Db::table('video')
            ->when($request->input('title'), function ($query) use ($request) {
                return $query->where('title', 'like', '%'.$request->input('title').'%');
            })
            //->where('movie_id',$request->get('movieId'))
           ->paginate(10);
        $items = $users->items();
        foreach ($items as $key=>$val){
            $items[$key] = ConversionUtils::underscoreToHump($val) ;
            if(strpos($items[$key]['poster'],'base64')===false){
                $items[$key]['poster'] = str_replace('assets/','',ConversionUtils::getPre(). $request->host() .$items[$key]['poster']) ;
            }

        }
        return  json($this->success('操作成功',$items,$users->total()));
    }

 

   
    /**
     * 检查验证码
     */
    public function show(Request $request,$id)
    {
        $data=  MovieVideo::find($id);
        if(empty($data)){
            return json($this->success('操作成功',[]));
        }
        $data = $data->toArray();
        $data=ConversionUtils::underscoreToHump($data) ;


        return json($this->success('操作成功',$data));
    }

    /**
     * 创建
     * @param Request $request
     * @return \support\Response
     */
    public function store(Request $request)
    {
        $Video = new Video();
        $Video->setRawAttributes(ConversionUtils::humpToUnderscore($request->all()));
        $Video->version=0;
        $Video->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 更新
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function update(Request $request,$id)
    {
        $Video = Video::find($id);
        if(empty($Video)){
            return  json($this->success('操作失败'));
        }
        $data = $request->all();
        $data = ConversionUtils::humpToUnderscore($data);
        unset($data['create_time'],$data['updateTime']);
        $Video->setRawAttributes($data);
        $Video->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function destroy(Request $request, $id)
    {
        Video::destroy($id) ;
        return  json($this->success('操作成功'));
    }

}
